package com.mailtest;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Testmail {

    // <bean id="mailSender"
    // class="org.springframework.mail.javamail.JavaMailSenderImpl">
    // <property name="host" value="${mail.host}" />
    // <property name="port" value="${mail.port}" />
    // <property name="username" value="${mail.username}" />
    // <property name="password" value="${mail.password}" />
    // <property name="javaMailProperties">
    // <props>
    // <prop key="mail.smtp.auth">
    // ${mail.smtp.auth}
    // </prop>
    // </props>
    // </property>
    // </bean>

    public void testSendQQemail() {

        JavaMailSenderImpl jmsi = new JavaMailSenderImpl();
        // 这个host相关的邮箱都会介绍
        jmsi.setHost("smtp.qq.com");
        jmsi.setPort(25);
        // 或者test@sina.com，注册的邮箱的用户名和密码
        jmsi.setUsername("2422515680@qq.com");
        jmsi.setPassword("tupletuple");
        Properties p = new Properties();
        // 邮件认证
        p.setProperty("mail.smtp.auth", "true");
        jmsi.setJavaMailProperties(p);

        MimeMessage mimeMessage = jmsi.createMimeMessage();
        // 编码UTF-8
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            // 邮箱名必须是已注册的，后面可以添加一个展示名，不添加默认为邮箱名
            helper.setFrom(jmsi.getUsername(), "用户找回密码");
            helper.setTo("122581919@qq.com");
            helper.setSubject("用户找回密码");
            helper.setText("邮箱找回ddddddddd密码哦！", true);
            jmsi.send(mimeMessage);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        
        
        
//        try {
//            // 构建简单邮件对象，见名知意
//            SimpleMailMessage smm = new SimpleMailMessage();
//            // 设定邮件参数
//            smm.setFrom(jmsi.getUsername());
//            smm.setTo("122581919@qq.com");
//            smm.setSubject("Hello world");
//            smm.setText("Hello world via spring mail sender");
//            jmsi.send(smm);
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }
    public void testSend85fenemail() {

        JavaMailSenderImpl jmsi = new JavaMailSenderImpl();
        // 这个host相关的邮箱都会介绍
        jmsi.setHost("smtp.85fen.com");
        jmsi.setPort(25);
        // 或者test@sina.com，注册的邮箱的用户名和密码
        jmsi.setUsername("root@85fen.com");
        jmsi.setPassword("aliabcd111");
        Properties p = new Properties();
        // 邮件认证
        p.setProperty("mail.smtp.auth", "true");
        jmsi.setJavaMailProperties(p);
//        p.put("mail.transport.protocol", "smtp");
//        p.put("mail.smtp.auth", "true");
//        p.put("mail.smtp.host", "85fen.com");

        MimeMessage mimeMessage = jmsi.createMimeMessage();
        // 编码UTF-8
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            // 邮箱名必须是已注册的，后面可以添加一个展示名，不添加默认为邮箱名
            helper.setFrom(jmsi.getUsername(), "用rrrr");
            helper.setTo("122581919@qq.com");
            helper.setSubject("用户找回密码");
            helper.setText("邮箱找回ddddddddd密码哦！", true);
            jmsi.send(mimeMessage);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        
        
        
//        try {
//            // 构建简单邮件对象，见名知意
//            SimpleMailMessage smm = new SimpleMailMessage();
//            // 设定邮件参数
//            smm.setFrom(jmsi.getUsername());
//            smm.setTo("122581919@qq.com");
//            smm.setSubject("Hello world");
//            smm.setText("Hello world via spring mail sender");
//            jmsi.send(smm);
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }
    public void testSend85fenemail2() {

        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "85fen.com");

        Session session = Session.getInstance(props/*
                                                    * , new Authenticator() {
                                                    * 
                                                    * protected
                                                    * PasswordAuthentication
                                                    * getPasswordAuthentication
                                                    * () {
                                                    * return new
                                                    * PasswordAuthentication
                                                    * ("suda", "a(R]5N@y");
                                                    * }
                                                    * }
                                                    */);

        session.setDebug(true);
        try {
            Message msg = new MimeMessage(session);
            msg.setText("你好吗？");
            msg.setFrom(new InternetAddress("admin@85fen.com"));
            Transport transport = session.getTransport();
            transport.connect("85fen.com", 25, "admin", "adminabcd111");
            // transport.send(msg, new Address[] { new
            // InternetAddress("01813467@163.com") });
            transport.sendMessage(msg, new Address[] { new InternetAddress("122581919@qq.com") });
            transport.close();
            // byte[] b = { 0x1a, 0x28 };
            // String k = "✄";
            // for (int i = 0; i < k.length(); i++) {
            // System.out.println(k.charAt(i));
            // }
            // System.out.println(new String(b));
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        Testmail testMail = new Testmail();
        testMail.testSend85fenemail();
//        testMail.testSendQQemail();

        System.out.println("end---------");
    }

}
